How to check empty array in Lodash?

by rachelle_funk , in category: JavaScript , 2 years ago

How to check empty array in Lodash?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by dmitrypro77 , 2 years ago

@rachelle_funk You can use isEmpty() to check any empty array in Lodash library, here is code as example:


1
2
3
4
5
6
7
import isEmpty from "lodash/isEmpty";

// Output: true
console.log(isEmpty([]))

// Output: false
console.log(isEmpty(["test"]))

Member

by cierra , a year ago

@rachelle_funk 

You can use the isEmpty() function in Lodash to check if an array is empty or not.


Here's an example:

1
2
3
4
5
6
7
const arr = [];

if (_.isEmpty(arr)) {
  console.log('Array is empty');
} else {
  console.log('Array is not empty');
}


The isEmpty() function returns a boolean value of true if the array is empty, otherwise it returns false.